package com.cstor.liang.web.controller;

import com.cstor.liang.config.Auth;
import com.cstor.liang.config.CurrentUser;
import com.cstor.liang.pojo.other.PageVo;
import com.cstor.liang.pojo.other.ResultJo;
import com.cstor.liang.pojo.entity.User;
import com.cstor.liang.pojo.other.UserMessageVo;
import com.cstor.liang.service.UserMessageService;
import com.cstor.liang.utils.WebUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.BindingResult;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

import javax.validation.Valid;

/**
 * Created by Administrator on 2017/9/6.
 */
@RestController
@RequestMapping("/userMessage")
public class UserMessageController {

    @Autowired
    private UserMessageService userMessageService;

    @RequestMapping(value = "/getUnreadCount", method = RequestMethod.GET, produces = "application/json;charset=utf-8")
    @Auth
    public ResultJo getUnreadCount(@CurrentUser User user) {
        return ResultJo.returnOk(
                userMessageService.getUnreadCount(user.getId()));
    }

    @RequestMapping(value = "/list", method = RequestMethod.GET, produces = "application/json;charset=utf-8")
    @Auth
    public ResultJo list(@CurrentUser User user, @Valid PageVo pageVo, BindingResult error) {
        //数据校验
        if (error.hasErrors()){
            //校验失败，收集所有错误
            String message = WebUtil.bindingResultCollect(error);
            return ResultJo.returnError(1, message);
        }

        return ResultJo.returnOk(
                userMessageService.listByUserId(
                        user.getId(), pageVo.getPageIndex(), pageVo.getPageSize()));
    }

    @RequestMapping(value = "/setToReaded", method = RequestMethod.POST, produces = "application/json;charset=utf-8")
    @Auth
    public ResultJo setToReaded(@CurrentUser User user, Long userMessageId) {
        //数据校验
        if (userMessageId == null){
            //校验失败，收集所有错误
            return ResultJo.returnError(1, "缺少userMessageId");
        }

        //设置用户消息为已读
        userMessageService.setToReaded(user.getId(), userMessageId);

        return ResultJo.returnOk("设置成功");
    }

    @RequestMapping(value = "/setAllToReaded", method = RequestMethod.POST, produces = "application/json;charset=utf-8")
    @Auth
    public ResultJo setAllToReaded(@CurrentUser User user) {

        //设置用户的全部消息为已读
        userMessageService.setAllToReaded(user.getId());

        return ResultJo.returnOk("设置成功");
    }

    @RequestMapping(value = "/add", method = RequestMethod.POST, produces = "application/json;charset=utf-8")
    @Auth(authority = "user_message_add")
    public ResultJo add(String userIds, @Valid UserMessageVo userMessageVo, BindingResult error) {
        //数据校验
        if (error.hasErrors()){
            //校验失败，收集所有错误
            String message = WebUtil.bindingResultCollect(error);
            return ResultJo.returnError(1, message);
        }
        if (userIds == null){
            //校验失败，收集所有错误
            return ResultJo.returnError(1, "缺少userIds");
        }

        //批量插入用户消息
        int row = userMessageService.add(userMessageVo, userIds);

        return ResultJo.returnOk("成功插入" + row + "条数据");
    }

    @RequestMapping(value = "/delete", method = RequestMethod.POST, produces = "application/json;charset=utf-8")
    @Auth
    public ResultJo delete(@CurrentUser User user, String userMessageIds) {
        //数据校验
        if (userMessageIds == null){
            //校验失败，收集所有错误
            return ResultJo.returnError(1, "缺少userMessageIds");
        }

        //批量删除用户消息
        int row = userMessageService.delete(userMessageIds, user.getId());

        return ResultJo.returnOk("成功删除" + row + "条数据");
    }


}
